<?php

m2import("model/entity/WineryOrderFormCatEntity.php");

/*
    Action configuration: 
        <conf:action mapping="wineryOrderFormCat" class="WineryOrderFormCatAction" methodmapping="true" bypassViewMapping="true">
        </conf:action>
*/
class WineryOrderFormCatAction extends phpinchains_WebAction {

    function index (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        $this->dispatch("wineryOrderFormCat::listing", $actionform);
    }

    function listPart (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        if ($actionform->get('redirect')) {
            $env->setSession('redirect_wineryorderformcat', $actionform->get('redirect'));
        }
        $entity = new WineryOrderFormCatEntity($actionform, null);
        $pagesize = 20;
        $actionform->set("pagesize", $pagesize);
        
        $pagername = 'wineryorderformcatPager';
        // Save pager value in session
        if ($actionform->get($pagername)) {
            $env->setSession($pagername, $actionform->get($pagername));
        }
        $actionform->set($pagername, $env->getSession($pagername));
        $offset = ($env->getSession($pagername) ? ($pagesize * (int) $env->getSession($pagername)) - $pagesize : 0);
        
        // sorting
        $sortingname = 'wineryorderformcatSorting';
        if ($actionform->get($sortingname) && $entity->hasField($actionform->get($sortingname), true)) {
            $sorting = $env->getSession($sortingname);
            if ($sorting[0] == $actionform->get($sortingname) && (empty($sorting[1]) || $sorting[1] == 'asc')) {
               $sorting[1] = 'desc';
            } else {
                $sorting = array($actionform->get($sortingname));
            }
            $env->setSession($sortingname, $sorting);
        }
        if (!$env->getSession($sortingname)) {
            $env->setSession($sortingname, array (  0 => 'wofcname',  1 => 'asc',));
        }
        $actionform->set('sorting', $env->getSession($sortingname));
        

        
            $env->setSession("searchactiveWineryOrderFormCat", false);
            $actionform->set("objList", $entity->findByFields(array(), 'AND', array(
                'limit' => $pagesize, 
                'joinreferenced' => true,
                'offset' => $offset,
                'orderby' => join(' ', $env->getSession($sortingname))
                )
            ));
            $count = $entity->findByFields(array(), 'AND', array(
                'fields' => "count(*) as count",
                'joinreferenced' => true,
                )
            );
        
        $actionform->set("count", $count['0']['count']);
        $actionform->set('wineryorderformcat_openCategories', $env->getSession("searchactiveWineryOrderFormCat")?$env->getSession('searchactiveWineryOrderFormCat_openCategories'):$env->getSession('wineryorderformcat_openCategories'));
    }

    function chooserListPart (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        if ($actionform->get('redirect')) {
            $env->setSession('redirect_chooserwineryorderformcat', $actionform->get('redirect'));
        }
        $entity = new WineryOrderFormCatEntity($actionform, null);
        $pagesize = 20;
        $actionform->set("pagesize", $pagesize);

        $pagername = 'wineryorderformcatPager';
        // Save pager value in session
        if ($actionform->get($pagername)) {
            $env->setSession($pagername, $actionform->get($pagername));
        }
        $actionform->set($pagername, $env->getSession($pagername));
        $offset = ($env->getSession($pagername) ? ($pagesize * (int) $env->getSession($pagername)) - $pagesize : 0);
        
        // sorting
        $sortingname = 'wineryorderformcatChooserSorting';
        if ($actionform->get($sortingname) && $entity->hasField($actionform->get($sortingname), true)) {
            $sorting = $env->getSession($sortingname);
            if ($sorting[0] == $actionform->get($sortingname) && (empty($sorting[1]) || $sorting[1] == 'asc')) {
               $sorting[1] = 'desc';
            } else {
                $sorting = array($actionform->get($sortingname));
            }
            $env->setSession($sortingname, $sorting);
        }
        if (!$env->getSession($sortingname)) {
            $env->setSession($sortingname, array (  0 => 'wofcname',  1 => 'asc',));
        }
        $actionform->set('sorting', $env->getSession($sortingname));


        
            $env->setSession("searchactiveWineryOrderFormCat", false);
            $actionform->set("objList", $entity->findByFields(array(), 'AND', array(
                'limit' => $pagesize,
                'joinreferenced' => true,
                'offset' => $offset,
                'orderby' => join(' ', $env->getSession($sortingname))
                )
            ));
            $count = $entity->findByFields(array(), 'AND', array(
                'fields' => "count(*) as count",
                'joinreferenced' => true,
                )
            );
        
        $actionform->set("count", $count['0']['count']);
        $actionform->set('wineryorderformcat_openCategories', $env->getSession("searchactiveWineryOrderFormCat")?$env->getSession('searchactiveWineryOrderFormCat_openCategories'):$env->getSession('wineryorderformcat_openCategories'));
    }


    function listing (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        $actionform->set('rootAction', 'wineryOrderFormCat::listing');
    }

    function chooser (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        if ($actionform->get('openerfield')) {
            $env->setSession('wineryOrderFormCat_openerfield', $actionform->get('openerfield'));
            $env->setSession('wineryOrderFormCat_openertext', $actionform->get('openertext'));
            $env->setSession('wineryOrderFormCat_databasefield', $actionform->get('databasefield'));
        }
        $actionform->set('openerfield', $env->getSession('wineryOrderFormCat_openerfield'));
        $actionform->set('openertext', $env->getSession('wineryOrderFormCat_openertext'));
        $actionform->set('databasefield', $env->getSession('wineryOrderFormCat_databasefield'));

        $actionform->set('rootAction', 'wineryOrderFormCat::chooser');
    }

    function createForm (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        $actionform->set('rootAction', 'wineryOrderFormCat::createForm');
        $actionform->set("formAction", "create");
        $this->_loadReferenced('create');
        return "form";
    }
    
    function create (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        $actionform->set('rootAction', 'wineryOrderFormCat::create');
        $this->constrainIncomingView("wineryOrderFormCat/form", "form");
        if ($actionform->get("abort")) {
            $actionform->remove('id');
            $this->_formDispatch($actionform);
        } elseif ($this->validate()) {
            $entity = new WineryOrderFormCatEntity($actionform, null);
            $formdata = $this->getFormdata("","",true);
            $entity->create($formdata);
            $actionform->remove('id');
            $env->setAttribute("actionmsg", "actionmsg_obj_created");
            $this->_formDispatch($actionform);
        } else {
            $this->dispatch("wineryOrderFormCat::createForm", $actionform);
        }
    }

    function edit (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        $actionform->set('rootAction', 'wineryOrderFormCat::edit');
        if ((int) $actionform->get("id")) {
            $entity = new WineryOrderFormCatEntity($actionform, null);
            $actionform->set("formAction", "save");
            $actionform->set("editObj", $entity->findByPrimaryKey($actionform->get("id"), array('joinreferenced' => true)));
            $this->_loadReferenced('edit');
            return "form";
        } else {
            $env->setAttribute("actionmsg", "actionmsg_no_id_edit");
            $this->_formDispatch($actionform);
        }
    }
    
    function save (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        $this->constrainIncomingView("wineryOrderFormCat/form", "form");
        if ($actionform->get("abort")) {
            $actionform->remove('id');
            $this->_formDispatch($actionform);
        } elseif ($this->validate()) {
            $entity = new WineryOrderFormCatEntity($actionform, null);
            $formdata = $this->getFormdata("", "", true);
            $entity->update($formdata);
            $env->setAttribute("actionmsg", "actionmsg_obj_saved");
            if ($actionform->get("save")) {
                $actionform->remove('id');
                $this->_formDispatch($actionform);
            } else {
                $this->dispatch("wineryOrderFormCat::edit", $actionform);
            }
        } else {
            $this->dispatch("wineryOrderFormCat::edit", $actionform);
        }
    }

    function delete (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        if ((int) $actionform->get("id")) {
            $entity = new WineryOrderFormCatEntity($actionform, null);
            
            $entity->delete((int) $actionform->get("id"));
            $actionform->remove('id');
            $env->setAttribute("actionmsg", "actionmsg_obj_deleted");
        } else {
            $env->setAttribute("actionmsg", "actionmsg_no_id_delete");
        }
        $this->_dispatchListing();
    }

    function show (env $env, phpinchains_ViewMapping $mapping, phpinchains_ActionForm $actionform) {
        $actionform->set('rootAction', 'wineryOrderFormCat::show');
        $id = !$actionform->get('id')?$env->getSession("selectedObj_wineryorderformcat_id"):$actionform->get('id');
        if ((int) $id) {
            $entity = new WineryOrderFormCatEntity($actionform, null);
            $this->_loadReferenced('show');
            $actionform->set("displayObj", $entity->findByPrimaryKey((int) $id, array("joinreferenced" => true, 'cascade-selfref' => true)));
        } else {
            $env->setAttribute("actionmsg", "actionmsg_no_id_show");
            $this->dispatch("wineryOrderFormCat::listing", $actionform);
        }
    }





    function _formDispatch ($actionform, $mode = '') {
        if ($actionform->get('caller') == 'show') {
            $this->dispatch("wineryOrderFormCat::show", $actionform);
        } elseif ($this->env->getSession('redirect_'.$mode.'wineryorderformcat')) {
            $this->dispatch($this->env->getSession('redirect_'.$mode.'wineryorderformcat'), $actionform);
        } else {
            $this->dispatch("wineryOrderFormCat::show", $actionform);
        }
    }

    function _dispatchListing ($mode = '') {
        $actionform = new phpinchains_Actionform();
        if ($this->env->getSession('redirect_'.$mode.'wineryorderformcat')) {
            $this->dispatch($this->env->getSession('redirect_'.$mode.'wineryorderformcat'), $actionform);
        } else {
            $this->dispatch('wineryOrderFormCat::listing', $actionform);
        }
    }
    
    function _handleFilter ($name) {
        if ($this->actionform->get("wineryOrderFormCatfilterReset") == "true") {
            $this->env->setSession($name, null);
            $this->env->setSession('searchactiveWineryOrderFormCat_openCategories', null);
        }
        if ($this->actionform->get($name) && !$this->actionform->get('reset')) {
            $fields = $this->actionform->get($name);
            $set = false;
            if (is_array($fields)) {
                foreach ($fields as $n => $v) {
                    if (strlen($v)) {
                        $set = true;
                        break;
                    }
                }
            } elseif (strlen($fields)) {
                $set = true;
            }
            if ($set) {
                $this->env->setSession($name, $this->actionform->get($name));
            }
        }
        $this->actionform->set($name, $this->env->getSession($name));
    }

    function _preProcess ($methodname, env $env, phpinchains_ViewMapping $viewmapping, phpinchains_ActionForm $actionform, $outsidecall) {
        // prevent setting rootAction from the outside
        if ($outsidecall) {
            $actionform->set('rootAction', '');
        }
        if (in_array($methodname, array("show", "edit")) && $outsidecall) {
            if ($actionform->get("id"))
                $env->setSession("selectedObj_wineryorderformcat_id", $actionform->get("id"));
        }
        
        // allow a preprocess method in the mix-ins
        if (method_exists($this, '_afterPreProcess')) {
            $this->_afterPreProcess($methodname, $env, $viewmapping, $actionform);
        }
    }

    function _loadReferenced ($caller) {

    }


}
    

?>